package com.example.myapplication.ui.borrow;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.myapplication.App;
import com.example.myapplication.R;
import com.example.myapplication.entity.BorrowAndLoan;
import com.example.myapplication.greendao.BorrowAndLoanDao;
import com.example.myapplication.greendao.DaoSession;

import java.util.Calendar;
import java.util.Date;

public class BorrowFragment extends Fragment {
    private int startYear, startMonth, startDay, endYear, endMonth, endDay;
    private Button start, end, b1;
    private Calendar calendar;
    private App app;
    private DaoSession daoSession;
    private Spinner spinner;
    private String element;
    private int s_position;
    private String payType;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_borrow, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        b1 = getActivity().findViewById(R.id.add);
        start = getActivity().findViewById(R.id.start);
        end = getActivity().findViewById(R.id.end);
        calendar = Calendar.getInstance();
        startYear = calendar.get(Calendar.YEAR);
        startMonth = calendar.get(Calendar.MONTH);
        startDay = calendar.get(Calendar.DAY_OF_MONTH);
        endYear = calendar.get(Calendar.YEAR);
        endMonth = calendar.get(Calendar.MONTH);
        endDay = calendar.get(Calendar.DAY_OF_MONTH);
        spinner = getActivity().findViewById(R.id.spinner1);
        spinner.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
        setListeners();
    }

    // 下拉框选择事件
    private class OnItemSelectedListenerImpl implements AdapterView.OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            element = parent.getItemAtPosition(position).toString();// 得到spanner的值
            s_position = position;
            switch (s_position) {
                case 0:
                    payType = "borrow";
                    break;
                case 1:
                    payType = "loan";
                    break;

            }
            Toast.makeText(getActivity(), "选择的元素是：" +
                    element, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    }


    private void setListeners() {
        Onclick onclick = new Onclick();
        b1.setOnClickListener(onclick);
        start.setOnClickListener(onclick);
        end.setOnClickListener(onclick);
    }

    private class Onclick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.add:
                    app = (App) getActivity().getApplication();
                    daoSession = app.getDaoSession();
                    EditText t1 = getActivity().findViewById(R.id.text_b1);
                    EditText t2 = getActivity().findViewById(R.id.text_b1);
                    double incomeVal = Double.parseDouble(t1.getText().toString());
                    String remark = t2.getText().toString();
                    BorrowAndLoanDao borrowAndLoanDao = daoSession.getBorrowAndLoanDao();
                    BorrowAndLoan borrowAndLoan = new BorrowAndLoan();
                    borrowAndLoan.setType(payType);
                    borrowAndLoan.setMoney(incomeVal);
                    borrowAndLoan.setRate(1);
                    borrowAndLoan.setStart(String.valueOf(start.getText()));
                    borrowAndLoan.setEnd(String.valueOf(end.getText()));
                    borrowAndLoan.setLoanRemarks(remark);
                    borrowAndLoanDao.save(borrowAndLoan);
                    Toast.makeText(getContext(), "插入成功", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.start:
                    new DatePickerDialog(getContext(),
                            startSetListener, startYear, startMonth, startDay).show();
                    break;
                case R.id.end:
                    new DatePickerDialog(getContext(),
                            endSetListener, endYear, endMonth, endDay).show();
                    break;
            }
        }

    }


    private DatePickerDialog.OnDateSetListener startSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            startYear = year;
            startMonth = monthOfYear;
            startDay = dayOfMonth;
            //更新Button上显示的日期
            updateDateStartDisplay();
        }
    };
    private void updateDateStartDisplay() {
        start.setText(new StringBuilder().append(startYear).append("-")
                .append(startMonth + 1).append("-").append(startDay));
    }

    private DatePickerDialog.OnDateSetListener endSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            endYear = year;
            endMonth = monthOfYear;
            endDay = dayOfMonth;
            //更新Button上显示的日期
            updateDateEndDisplay();
        }
    };




    private void updateDateEndDisplay() {
        end.setText(new StringBuilder().append(endYear).append("-")
                .append(endMonth + 1).append("-").append(endDay));
    }
}

